package com.lcp.controller;

import com.lcp.common.Result;
import com.lcp.entity.ChronicDisease;
import com.lcp.entity.User;
import com.lcp.service.ChronicDiseaseService;
import com.lcp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chronic-diseases")
public class ChronicDiseaseController {

    @Autowired
    private ChronicDiseaseService chronicDiseaseService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result<ChronicDisease> getById(@PathVariable Long id) {
        return Result.success(chronicDiseaseService.getById(id));
    }

    @GetMapping("/current")
    public Result<List<ChronicDisease>> getCurrentUserDiseases() {
        User currentUser = userService.getCurrentUser();
        return Result.success(chronicDiseaseService.listByUserId(currentUser.getId()));
    }

    @PostMapping
    public Result<ChronicDisease> create(@RequestBody ChronicDisease disease) {
        User currentUser = userService.getCurrentUser();
        disease.setUserId(currentUser.getId());
        return Result.success(chronicDiseaseService.create(disease));
    }

    @PutMapping("/{id}")
    public Result<ChronicDisease> update(@PathVariable Long id, @RequestBody ChronicDisease disease) {
        disease.setId(id);
        return Result.success(chronicDiseaseService.update(disease));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        chronicDiseaseService.delete(id);
        return Result.success();
    }

    @GetMapping("/status/{status}")
    public Result<List<ChronicDisease>> listByStatus(@PathVariable String status) {
        User currentUser = userService.getCurrentUser();
        return Result.success(chronicDiseaseService.listByStatus(currentUser.getId(), status));
    }

    @GetMapping("/upcoming-follow-ups")
    public Result<List<ChronicDisease>> listUpcomingFollowUps() {
        User currentUser = userService.getCurrentUser();
        return Result.success(chronicDiseaseService.listUpcomingFollowUps(currentUser.getId()));
    }

    @PutMapping("/{id}/follow-up-status")
    public Result<Void> updateFollowUpStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        chronicDiseaseService.updateFollowUpStatus(id, status);
        return Result.success();
    }
} 